package htmlAnalysis;

import htmlAnalysis.entity.TableTd;
import htmlAnalysis.entity.TableTr;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述:Html解析
 *
 * @author yanzhengwu
 * @create 2020-12-28 10:41
 */
public class HtmlAnalysis {
    public static void main(String[] args) {


        String htmlStr =
                "<table id=testTable >"
                        + "<tr height=\"300\"> "
                        + "<th>标题1</th>"
                        /*+ "<td width=\"120\" color=\"red\">这里是要获取的数据1</td>"
                        + "<td width=\"120\" color=\"red\">这里是要获取的数据2</td>"
                        + "<td width=\"120\" color=\"red\">这里是要获取的数据3</td>"*/
                        + "</tr>"
                        + "<tr height=\"400\">"
                        + "<th>标题2</th>"
                        + "<td width=\"220\" color=\"red\">这里是要获取的数据5</td>"
                        + "<td width=\"220\" color=\"red\">这里是要获取的数据6</td>"
                        + "<td width=\"220\" color=\"red\">这里是要获取的数据7</td>"
                        + "<td width=\"240\" color=\"red\">这里是要获取的数据8</td>"
                        + "</tr>"
                        + "</table>";
        Document doc = Jsoup.parse(htmlStr);
        Elements elements = doc.select("table");
        HtmlAnalysis htmlAnalysis = new HtmlAnalysis();

        try {
            htmlAnalysis.startAnalysis(elements);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //System.out.println(htmlAnalysis.tableTr);
        System.out.println(htmlAnalysis.dataList);

    }
    /**Excel合并单元格对应的矩阵*/
    //private List<List<Integer>> tableMatrix=new ArrayList<List<Integer>>();
    /**数据集合*/
    private List<TableTr> dataList = new ArrayList<TableTr>();
    /**行*/
    private TableTr tableTr;
    /**总行数*/
    private int rowSumSize;
    /**总列数*/
    private int cellSumSize;
    /**当前行*/
    private int presentRowSize;
    /**当前列*/
    private int presentCellSize;

    public void startAnalysis(Elements elements) throws IllegalAccessException {
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            if ("tr".equals(element.tagName())) {
                Elements tr = element.children();
                if (null != tr) {
                    //创建新行
                    this.tableTr = new TableTr();
                    //总行数
                    this.rowSumSize = elements.size();
                    //总列数
                    this.cellSumSize=tr.size();
                    //当前行
                    this.presentRowSize = i;
                    //设置行标签属性
                    setTableProperty(tableTr, element);
                    //添加一个列
                    tableTr.setCellList(new ArrayList<TableTd>());
                    //将行加入
                    dataList.add(tableTr);
                    System.out.println("总行数：" + rowSumSize);
                }
            }
            if ("td".equals(element.tagName())) {
                Elements td = element.children();
                if (null != td) {
                    //创建新列
                    TableTd tableTd = new TableTd();
                    //当前列
                    this.presentCellSize = i;
                    //添加标签内容
                    tableTd.setContent(element.text());
                    //设置标签属性
                    setTableProperty(tableTd, element);
                    //将列添加入行
                    tableTr.getCellList().add(tableTd);

                   /* System.out.println("第" + (presentRowSize + 1) + "行");
                    System.out.println("第：" + (this.presentCellSize + 1) + "列");
                    System.out.println("数据：" + element.text());*/

                }
            }
            if (null != element.children() && element.children().size() > 0) {
                startAnalysis(element.children());
            }

        }
    }

    public <T> void setTableProperty(T t, Element element) throws IllegalAccessException {
        for (Attribute attribute : element.attributes()) {
            for (Field field : t.getClass().getDeclaredFields()) {
                if (field.getName().equals(attribute.getKey())) {
                    field.setAccessible(true);//关闭安全检查
                    field.set(t, attribute.getValue());
                    field.setAccessible(false);//开启安全检查
                }
            }
            String key = attribute.getKey();
            String value = attribute.getValue();
            //System.out.println("属性列表：" + "key==>" + key + ",value==>" + value);
            //System.out.println("----------------------------------------------------");
        }

    }


}
